<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>受控组件</title>
    <style>
        
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            //1.声明 state 属性
            state = {
                uname: '',
                upass: ''
            }
            render(){
                return <div style={{paddingLeft: '20px'}}>
                        <h2>登录</h2>
                        <form action="">
                            {/* 2. 设置 value 的值为 state 其中的属性值,  并且必须要添加 onChange 事件*/}
                            用户名: <input type="text" value={this.state.uname}  name="uname"   onChange={this.saveUserData} /> <br/>    
                            密　码: <input type="password" value={this.state.upass}  name="upass"   onChange={this.saveUserData} /> <br />
                            <button onClick={this.login}>登录</button>
                        </form>
                    </div>
            }

            //保存用户名的事件回调
            saveUserData = (e) => {
                //获取表单输入的值
                let v = e.target.value;
                //获取表单中对应的 『属性名』
                let name = e.target.getAttribute('name');
                this.setState({
                    [name]: v
                })
            }

            login = (e) => {
                //获取用户名
                console.log('当前的用户名为: '+this.state.uname)
                console.log('当前的密码为: '+this.state.upass)

                e.preventDefault();
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));

    </script>
</body>
</html>